<template>
  <div class='container'>
     <div class="app-container">
      <el-card shadow="never">
        <!-- 搜索 -->
        <el-form ref="ruleForm" :inline="true" :model="form">

            <el-row :gutter="20">
              <el-col :span="5">
                <el-form-item label="关键字">
                  <el-input
                    style="width: 180px;"
                    placeholder="请输入"
                    class="filter-item"
                    v-model="form.keyword"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="状态">
                  <el-select
                    class="filter-item"
                    style="width: 190px;"
                    v-model="form.state"
                  >

                    <el-option label="已启用" value="1"></el-option>
                    <el-option label="已禁用" value="0"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                  <el-form-item>
                    <el-button @click="resetForm">清除</el-button>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="getList">搜索</el-button>
                  </el-form-item>
              </el-col>
              <el-col :span="5" :offset="4">
                <el-button
                  class="filter-item fr"
                  size="small"
                  style="margin-left: 10px;"
                  @click="handleChange('add')"
                  type="success"
                  icon="el-icon-edit"
                >新增技巧</el-button>
              </el-col>
            </el-row>

        </el-form>
        <!-- 数据记录 -->
        <el-alert
          type="info"
          class="alert"
          :closable="false"
          show-icon
        >
          <template #title>
            数据共{{total}}条
          </template>
          </el-alert>
        <!-- end -->
        <!-- 数据列表 -->
        <el-table
          :data="tableData"
          element-loading-text="给我一点时间"
          fit
          highlight-current-row
          style="width: 100%"
        >
          <el-table-column align="center" label="序号" type="index">
          </el-table-column>
          <el-table-column align="center" label="文章标题">
            <template #default="{row}">
              {{row.title}}
              <a @click.prevent="openVideo(row.videoURL)" v-if="row.videoURL" href="#" class="el-icon-film"></a>
            </template>

          </el-table-column>
          <el-table-column align="center" label="阅读数" prop="visits">

          </el-table-column>
          <el-table-column label="录入人" prop="username">

          </el-table-column>
          <el-table-column align="center" label="录入时间">
            <template #default="{row}">
              {{row.createTime | parseTimeByString}}
            </template>

          </el-table-column>

          <el-table-column align="center" label="状态">
            <template #default="{row}">
            {{row.state===1 ? '已启用' : '已禁用'}}
            </template>
          </el-table-column>

          <!-- 操作按钮 -->
          <el-table-column
            align="center"
            label="操作"
            width="280"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button type="text" @click="previewArticle(scope.row)">预览</el-button>
              <el-button type="text"  @click="handleStatus(scope.row)">
                <span v-if="scope.row.state==0">启用</span>
                <span v-else>禁用</span>
              </el-button>
              <el-button type="text" :disabled="scope.row.state !== 1" @click="handleChange('edit',scope.row)">修改</el-button>
              <el-button
                :disabled="scope.row.state !== 1"
                type="text"
                @click="delArticle(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- end -->
        <!-- 分页 -->
        <div class="pagination">
            <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="page"
              :total="total"
              :page-size="pagesize"
              :page-sizes="[5,10,20,30, 50]"
              layout=" prev, pager, next,sizes, jumper"

            ></el-pagination>

        </div>
      </el-card>
    </div>
        <!-- 视频 -->
    <div class="video-preview" v-if="videoUrl">
      <div class="close" @click="closeVideo()"><span class="el-icon-close"></span></div>
      <div class="box">
        <video ref="video" :src="videoUrl"></video>
      </div>
    </div>
    <!-- 文章预览 -->
    <ArticlesPreview :previewArticleDialog.sync="previewArticleDialog" :articlePre="articlePre"></ArticlesPreview>
    <!-- 新增或修改文章 -->
    <ArticlesAdd :addArticleDialog.sync="addArticleDialog" ref="addArticles"  @uploadData="getList"></ArticlesAdd>
  </div>
</template>

<script>
import { list, changeState, remove, detail } from '@/api/hmmm/articles.js'
import ArticlesPreview from '../components/articles-preview.vue'
import ArticlesAdd from '../components/articles-add.vue'
export default {
  components: { ArticlesPreview, ArticlesAdd },
  name: 'articles',
  data () {
    return {
      form: {
        keyword: '',
        state: ''
      },
      tableData: [],
      page: 1,
      pagesize: 10,
      total: 10,
      videoUrl: null,
      previewArticleDialog: false,
      addArticleDialog: false,
      articlePre: {}
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 清除按钮
    resetForm () {
      this.form.keyword = ''
    },
    handleSizeChange (val) {
      this.pagesize = val
      this.page = 1
      this.getList()
      // console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      this.page = val
      this.getList()
      // console.log(`当前页: ${val}`)
    },
    // 发送请求获取列表
    async getList () {
      let reqData = {}
      if (!this.form.state) {
        reqData = {
          page: this.page,
          pagesize: this.pagesize,
          keyword: this.form.keyword
        }
      } else {
        reqData = {
          page: this.page,
          pagesize: this.pagesize,
          keyword: this.form.keyword,
          state: +this.form.state
        }
      }
      const { data } = await list(reqData)
      console.log(data)
      this.total = data.counts
      this.tableData = data.items
    },
    // 打开播放器按钮
    openVideo (url) {
      this.videoUrl = url
      // 获取dom元素应该在nextTick中
      this.$nextTick(() => {
        this.$refs.video.play()
      })
    },
    // 关闭视频播放
    closeVideo () {
      console.dir(this.$refs.video)
      // this.$refs.video.pause()
      this.videoUrl = null
    },
    // 修改启用禁用状态
    async handleStatus (row) {
      const data = {
        id: row.id,
        state: row.state === 1 ? 0 : 1
      }
      const res = await changeState(data)
      if (res.status === 200) {
        this.$message.success('操作成功')
      }
      this.getList()
    },
    // 预览文章
    previewArticle (row) {
      // console.log(row)
      this.previewArticleDialog = true
      this.articlePre = row
    },
    // 新增或修改文章
    async handleChange (btnName, data = {}) {
      this.addArticleDialog = true
      if (btnName === 'add') {
        this.$refs.addArticles.dialogTitle = '新增文章'
      } else {
        this.$refs.addArticles.dialogTitle = '修改文章'
        console.log(data)
        const res = await detail(data)
        this.$refs.addArticles.formData = res.data
      }
    },
    // 删除文章
    delArticle (id) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        type: 'warning'
      }).then(async () => {
        const res = await remove({ id })
        console.log(res)
        if (res.status === 200) {
          this.getList()
          this.$message.success('删除成功!')
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }

  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.el-table th {
  background-color: #fafafa;
}
.el-table th.is-leaf {
  border-bottom: 2px solid #e8e8e8;
}
.disabled td {
  background-color: #f9f9f9;
  color: #c1c1c1;
}
.disabled .el-button--primary,
.disabled .el-button--danger {
  background-color: #dbdada;
  border: 1px solid #dbdada;
  color: #999;
  cursor: not-allowed;
}
.pagination{
  display: flex;
  justify-content: end;
}
.el-icon-film{
  color: blue;
}
.video-preview{
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,.3);
  overflow: hidden;
  z-index: 9999;
  .close{
    width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%,0);
    background: rgba(0,0,0,0.5);
    box-shadow: 0 0 5px rgba(0,0,0,0.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
  }
  .box{
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    video{
      width: 100%;
      height: 100%;
    }
  }
}
</style>
